import Constants

def assembler(instruction):
    """
    对初步分割的指令进行编译
    :param instruction: 分割好的指令，如：['c.addi4spn', '1', '32']
    :return: instructionConverted：转换为二进制代码的指令，如：0000100100000000
    """
    judgeInstructionFormat = Constants.confirmInstructionFormat(instruction[0])
    instructionConverted = []
    func = Constants.switch.get(judgeInstructionFormat)
    if func is not None:
        instructionConverted = func(instruction)
    return instructionConverted


f = open("./ProgramBinary.txt", "w")
for instruction in open('./Program.txt', "r"):
    instructionSplit = instruction.strip().replace(",", " ").replace("(", " ").replace(")", " ").strip().split(" ")
    print(instructionSplit)
    instructionAssembler = assembler(instructionSplit)
    print(instructionAssembler)
    f.write(instructionAssembler + "\n")
    pass
f.close()
